package fr.uvsq.isty.bdh.hchess.gui.menu;

import fr.uvsq.isty.bdh.hchess.gui.game.GameDisplay;
import fr.uvsq.isty.bdh.hchess.gui.MainWindow;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JPanel;




/**
 * The main menu containing several options allowing to go on other menus. * 
 * 
 * @author Nathan Ramoly
 * @version 08-06-12
 */
public class MainMenu extends JPanel{

	
	//Attributes
	
	/**
	 * Parent window
	 */
	private MainWindow MW;
	
	/**
	 * Background image
	 */
	private Image bg;
	
	/**
	 * Button to acces the local menu.
	 */
	private JButton Bsolo;
	
	/**
	 * Online menu
	 */
	private JButton Bmulti;
	
	/**
	 * Elo menu
	 */
	private JButton Belo;
	
	/**
	 * Acceding the replay menu
	 */
	private JButton Breplay;
	
	/**
	 * Exit
	 */
	private JButton Bquit;
	
	
	//Methods
	
	/**
	 * Constructor, where everything is done.
	 * @param parent the parent window containing this Panel. To be able to change the content.
	 */
	public MainMenu(MainWindow parent)
	{
		this.MW = parent;
		this.setPreferredSize(new Dimension(GameDisplay.DEFAULT_WIDTH, GameDisplay.DEFAULT_HEIGHT));
		
		GridBagConstraints gbc = new GridBagConstraints();
		
		this.setLayout(new GridLayout(10, 3, 200, 25));
		
		//Background
		try {
			bg = ImageIO.read(
                                getClass().getClassLoader()
                                .getResource("menu/menu.jpg"));
		} catch (IOException e1) {			
			e1.printStackTrace();
		}
		
		//Button Solo
		Bsolo = new CustomButton("Local");	
                
                this.add(Box.createHorizontalGlue());
                this.add(Box.createHorizontalGlue());
                this.add(Box.createHorizontalGlue());
                this.add(Box.createHorizontalGlue());
                this.add(Box.createHorizontalGlue());
                this.add(Box.createHorizontalGlue());
                
		
		Bsolo.setAlignmentX(Component.CENTER_ALIGNMENT);	
		this.add(Box.createHorizontalGlue()); // upper space
		this.add(Bsolo);
                this.add(Box.createHorizontalGlue());
		//this.add(Box.createHorizontalGlue()); //space between button
		
		Bsolo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	MW.setContentPane(new SoloMenu(MW));
            	MW.setVisible(true);
            	MW.repaint();
            }
        });	
		
		//Button multi
		Bmulti = new CustomButton("Online");
		
		Bmulti.setAlignmentX(Component.CENTER_ALIGNMENT);	
                this.add(Box.createHorizontalGlue());
		this.add(Bmulti);
                this.add(Box.createHorizontalGlue());
		
		
		//Temporaire
		Bmulti.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
           
            	MW.setContentPane(new OnlineMenu(MW));
            	MW.setVisible(true);
            	MW.repaint();
            }
        });	
		
		
		//Button Elo
		Belo = new CustomButton("Ranking");
		
		Belo.setAlignmentX(Component.CENTER_ALIGNMENT);	
                this.add(Box.createHorizontalGlue());
		this.add(Belo);
                this.add(Box.createHorizontalGlue());
		
		Belo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	MW.setContentPane(new EloMenu(MW));
            	MW.setVisible(true);
            	MW.repaint();
            }
        });	
		
		
		//Button replay
		Breplay = new CustomButton("Replay");
		
		Breplay.setAlignmentX(Component.CENTER_ALIGNMENT);
                this.add(Box.createHorizontalGlue());
		this.add(Breplay);
                this.add(Box.createHorizontalGlue());
		
		Breplay.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	MW.setContentPane(new ReplayMenu(MW));
            	MW.setVisible(true);
            	MW.repaint();
            }
        });	
		
		
		//Button quit
		Bquit = new CustomButton("Exit");
		
		Bquit.setAlignmentX(Component.CENTER_ALIGNMENT);
                this.add(Box.createHorizontalGlue());
		this.add(Bquit);
		this.add(Box.createHorizontalGlue()); // bottom space
		
		Bquit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            	System.exit(0);
            }
        });	

		
	}
	
	 @Override
	 public void paintComponent(Graphics g){
		g.drawImage(bg, 0, 0, this.getWidth(), this.getHeight(), this);
	 }
}
